package ar.edu.unicen.exa.isistan.archassis.javatoxml;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ar.edu.unicen.exa.isistan.archassis.classreader.CanonicalClassNameResolver;
import ar.edu.unicen.exa.isistan.archassis.ia.seed.VariableSeed;

public class VariableTypeResolver implements Cloneable {
	private Map<String,String> variables;
	private CanonicalClassNameResolver canonicalClassNameResolver;

	public VariableTypeResolver(Map<String,String> variables, CanonicalClassNameResolver canonicalClassNameResolver) {
		this.variables = variables;
		this.canonicalClassNameResolver = canonicalClassNameResolver;
	}
	public VariableTypeResolver(List<VariableSeed> javaClass_variables, CanonicalClassNameResolver canonicalClassNameResolver) {
		this.canonicalClassNameResolver = canonicalClassNameResolver;
		variables = new HashMap<String, String>();
		for (VariableSeed variableSeed : javaClass_variables) {
			put(variableSeed.getName(), variableSeed.getType());
		}
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		Map<String,String> variablesCopy = new HashMap<String, String>();
		variablesCopy.putAll(this.variables);
		return new VariableTypeResolver(variablesCopy, this.canonicalClassNameResolver);
	}
	public void put(String vName, String vType) {
		String absoluteType = canonicalClassNameResolver.resolve(vType);
		variables.put(vName, absoluteType);
		
	}
//	public String get(String vName) {
//		String result = get2(vName);
//		//System.out.println(vName + "-->" + result);
//		return result;
//	}
	public String get(String vName) {
		if (vName == null) {
			 return "this";
		}
		String result = variables.get(vName);
		if (result != null) {
			return result;
		}
		String resolved = canonicalClassNameResolver.resolve(vName);
		if (!resolved.equals(vName)) {
			//era un llamado estatico
			return resolved;
		}
		if ("System".equals(vName)) {
			return "java.lang.System";
		}
		if ("Character".equals(vName)) {
			return "java.lang.Character";
		}
		if ("Math".equals(vName)) {
			return "java.lang.Math";
		}
		System.err.println("y esto? " + vName);
		//ni idea
		return vName; 
	}

}
